golang http request with cookie

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
   url := "http://127.0.0.1:8998"
req, err := http.NewRequest("POST", url, nil)
if err != nil {
log.Error(err)
return
}

cookie := http.Cookie{Name: "cookie_name", Value: "cookie_value"}
req.AddCookie(&cookie)
var client = &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Error(err)
return
}
defer resp.Body.Close()

ret, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}